package com.tronsis.distribution.activity.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.CountDownTimerUtil;
import com.tronsis.library.util.KeyboardUtils;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPasswordActivity extends ToolBarBaseActivity {

    @BindView(R.id.iv_show_password)
    ImageView mIvShowPassword;
    private boolean isOpenEye = false;
    @BindView(R.id.et_reset_phone)
    EditText mEtResetPhone;
    @BindView(R.id.et_check_code)
    EditText mEtCheckCode;
    @BindView(R.id.tv_get_check_code)
    TextView mTvGetCheckCode;
    @BindView(R.id.et_reset_password_value)
    EditText mEtResetPasswordValue;
    @BindView(R.id.btn_confirm_reset)
    Button mBtnConfirmReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);
        setTitle(true, "重置密码");

    }

    @OnClick(R.id.tv_get_check_code)
    void getCheckCodeClick() {
        String phone = mEtResetPhone.getText().toString();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showMessage(mContext, "请输入手机号");
            return;
        }


        if (!StringUtil.isMobile(phone)) {
            ToastUtil.showMessage(mContext, "请输入正确的手机号");
            return;
        }

        httpGetCheckCode(phone);
    }


    @OnClick(R.id.iv_show_password)
    void showPasswordClick() {
        if (!isOpenEye) {
            mEtResetPasswordValue.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            mEtResetPasswordValue.setSelected(true);
            isOpenEye = true;
            mIvShowPassword.setImageResource(R.drawable.icon_eye_sel);
        } else {
            mEtResetPasswordValue.setTransformationMethod(PasswordTransformationMethod.getInstance());
            mEtResetPasswordValue.setSelected(false);
            isOpenEye = false;
            mIvShowPassword.setImageResource(R.drawable.icon_eye_gray);
        }
        mEtResetPasswordValue.setFocusable(true);
        mEtResetPasswordValue.setFocusableInTouchMode(true);
        mEtResetPasswordValue.requestFocus();

    }

    @OnClick(R.id.btn_confirm_reset)
    void confirmResetClick() {

        String phone = mEtResetPhone.getText().toString();
        String checkCode = mEtCheckCode.getText().toString();
        String password = mEtResetPasswordValue.getText().toString();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showMessage(mContext, "请输入手机号");
            return;
        }

        if (TextUtils.isEmpty(checkCode)) {
            ToastUtil.showMessage(mContext, "请输入验证码");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.showMessage(mContext, "请输入密码");
            return;
        }

        if (!StringUtil.isWellFormedPwd(password)) {
            ToastUtil.showMessage(mContext, "密码由大小写字母以及数字组成，长度为8-24位");
            return;
        }



        KeyboardUtils.hideSoftInput(this);
        httpForgotPassword(phone, password,checkCode);

    }

    private void httpGetCheckCode(String phone) {
        try {
            UserBizFactory.createBiz().getSmsCode(this, phone, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ToastUtil.showMessage(mContext, "短信验证码已发送");
                    new CountDownTimerUtil(mTvGetCheckCode, 60 * 1000, 1000).start();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void httpForgotPassword(String phone, String password, String checkCode) {
        try {
            UserBizFactory.createBiz().forgetPassword(this, phone, password,checkCode, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ToastUtil.showMessage(mContext, "密码重置成功");
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }


}
